﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConstantCMapEditor3
{
    public partial class Dropbox : Form
    {
        List<System.Windows.Forms.PictureBox> PictureBoxSpriteList = new List<System.Windows.Forms.PictureBox>();
        List<System.Drawing.Point> pictureSpritePosition = new List<System.Drawing.Point>();
        List<System.Windows.Forms.PictureBox> PictureBoxMapList = new List<System.Windows.Forms.PictureBox>();
        List<System.Drawing.Point> pictureMapPosition = new List<System.Drawing.Point>();
        
        public enum Mode { Map, Sprite }
        Mode nowMode = Mode.Map;
   
        public Dropbox()
        {
            InitializeComponent();
        }

        public void Dropbox_Load(object sender, EventArgs e)
        {
            creatPictureBox();
            iniPictureBox();
            endIniPictureBox();
            addAllMapControl();
            vScrollBar1.Maximum = 100;
            switch (Game1.Mode)
            {
                case EditorMode.Map_Mode:
                    switchMode(Mode.Map);
                    break;
                case EditorMode.Sprite_Mode:
                    switchMode(Mode.Sprite);
                    break;
            }
        }

        public void creatPictureBox()
        {
            for (int i = 0; i < Game1.CandidateDeclaration.Count; i++)
            {
                this.PictureBoxMapList.Add(new System.Windows.Forms.PictureBox());
                ((System.ComponentModel.ISupportInitialize)(this.PictureBoxMapList[i])).BeginInit();
            }
            for (int i = 0; i < Game1.CandidateNormalSprite.Count; i++)
            {
                this.PictureBoxSpriteList.Add(new System.Windows.Forms.PictureBox());
                ((System.ComponentModel.ISupportInitialize)(this.PictureBoxSpriteList[i])).BeginInit();
            }
        }
        public void endIniPictureBox()
        {
            for (int i = 0; i < PictureBoxSpriteList.Count; i++)
            {
                ((System.ComponentModel.ISupportInitialize)(this.PictureBoxSpriteList[i])).EndInit();
            }
            for (int i = 0; i < PictureBoxMapList.Count; i++)
            {
                ((System.ComponentModel.ISupportInitialize)(this.PictureBoxMapList[i])).EndInit();
            }
        }
        public void iniPictureBox()
        {
            for (int i = 0; i < PictureBoxMapList.Count; i++)
            {
                this.PictureBoxMapList[i].Location = new System.Drawing.Point(2 + 60 * (i % 3), 1 + 60 * (i / 3));
                pictureMapPosition.Add(PictureBoxMapList[i].Location);
                this.PictureBoxMapList[i].Size = new System.Drawing.Size(60, 60);
                this.PictureBoxMapList[i].SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                if (Game1.CandidateDeclaration[i].sprite is AnimateSprite)
                {
                    AnimateSprite tempSprite = ((AnimateSprite)Game1.CandidateDeclaration[i].sprite);
                    this.PictureBoxMapList[i].Image = ResourceManager.ConvertTexture2DToImage(Game1.CandidateDeclaration[i].sprite.Texture, tempSprite._resourceRectangleList[0]);
                }
                else
                    this.PictureBoxMapList[i].Image = ResourceManager.ConvertTexture2DToImage(Game1.CandidateDeclaration[i].sprite.Texture, Game1.CandidateDeclaration[i].sprite.ResourceRectangle);
                this.PictureBoxMapList[i].Click += pictureBoxMap_ClickMap;
            }

            for (int i = 0; i < PictureBoxSpriteList.Count; i++)
            {
                this.PictureBoxSpriteList[i].Location = new System.Drawing.Point(2 + 60 * (i % 3), 1 + 60 * (i / 3));
                pictureSpritePosition.Add(PictureBoxSpriteList[i].Location);
                this.PictureBoxSpriteList[i].Size = new System.Drawing.Size(60, 60);
                this.PictureBoxSpriteList[i].SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                if (Game1.CandidateNormalSprite[i].sprite is AnimateSprite)
                {
                    AnimateSprite tempSprite = ((AnimateSprite)Game1.CandidateNormalSprite[i].sprite);
                    this.PictureBoxSpriteList[i].Image = ResourceManager.ConvertTexture2DToImage(Game1.CandidateNormalSprite[i].sprite.Texture, tempSprite._resourceRectangleList[0]);
                }
                else
                    this.PictureBoxSpriteList[i].Image = ResourceManager.ConvertTexture2DToImage(Game1.CandidateNormalSprite[i].sprite.Texture, Game1.CandidateNormalSprite[i].sprite.ResourceRectangle);
                this.PictureBoxSpriteList[i].Click += pictureBoxSprite_ClickSprite;
            }
        }

        public void switchMode(Mode mode)
        {
            removeAllControl();
            switch(mode)
            {
                case Mode.Map:
                    addAllMapControl();
                    break;
                case Mode.Sprite:
                    addAllSpriteControl();
                    break;
            }
        }
       
        public void addAllSpriteControl()
        {
            for (int i = 0; i < PictureBoxSpriteList.Count; i++)
            {
                this.Controls.Add(PictureBoxSpriteList[i]);
            }
        }

        public void addAllMapControl()
        {
            for (int i = 0; i < PictureBoxMapList.Count; i++)
            {
                this.Controls.Add(PictureBoxMapList[i]);
            }
        }

        public void removeAllControl()
        {
            this.Controls.Clear();
            this.Controls.Add(vScrollBar1);
        }

        private void pictureBoxSprite_ClickSprite(object sender, EventArgs e)
        {
            int i = PictureBoxSpriteList.IndexOf((System.Windows.Forms.PictureBox)sender);
            Game1.SpriteEditorSelectedIndex = i;
            Game1.UISelected = Game1.CandidateNormalSprite[i];
        }
        private void pictureBoxMap_ClickMap(object sender, EventArgs e)
        {
            int i = PictureBoxMapList.IndexOf((System.Windows.Forms.PictureBox)sender);
            Game1.MapEditorSelectedIndex = i;
            Game1.UISelected = Game1.CandidateDeclaration[i];
        }


        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            for (int i = 0; i < PictureBoxSpriteList.Count; i++)
            {
                PictureBoxSpriteList[i].Location = new System.Drawing.Point(pictureSpritePosition[i].X, pictureSpritePosition[i].Y - 60 * e.NewValue);
            }
            for (int i = 0; i < PictureBoxMapList.Count; i++)
            {
                PictureBoxMapList[i].Location = new System.Drawing.Point(pictureMapPosition[i].X, pictureMapPosition[i].Y - 60 * e.NewValue);
            }
        }

        private void Dropbox_FormClosed(object sender, FormClosedEventArgs e)
        {
            Game1.DropboxShowed = false;
        }

    }
}
